package com.sharemarking.wa.platform.mapper;


import com.sharemarking.wa.common.entity.OrgVerify;
import com.sharemarking.wa.platform.dto.receiveOrderManagement.OrderInfoDto;
import com.sharemarking.wa.platform.dto.receiveOrderManagement.OrderListDto;
import com.sharemarking.wa.platform.dto.receiveOrderManagement.OrderWasteListDto;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author dxy
 */
@Repository
public interface ReceiveOrderManagementMapper {

    @Select("SELECT d.waste_id \n" +
            "FROM wa_order o\n" +
            "LEFT JOIN wa_order_details d on o.id = d.order_id\n" +
            "LEFT JOIN sys_org so ON so.id = o.send_org_id\n" +
            "LEFT JOIN wa_org_waste ow ON ow.id = d.waste_id\n" +
            "LEFT JOIN (\n" +
            "SELECT d.parent_order_id,d.waste_id,d.name,sum(d.weight) as weight\n" +
            "FROM `wa_order_details` d LEFT JOIN wa_order wo ON wo.id = d.order_id  \n" +
            "where 1=1\n" +
            "and d.parent_order_id is not null AND wo.state != 10 \n" +
            "GROUP BY d.parent_order_id,d.waste_id,d.name\n" +
            ") x on d.order_id = x.parent_order_id and d.waste_id = x.waste_id\n" +
            "where 1=1\n" +
            "and o.type = 0\n" +
            "and o.receive_org_id IS null\n" +
            "and o.state = 0\n" +
            "and  IFNULL(x.weight,'0') < d.weight\n" +
            "and d.waste_type_name in (\n" +
            "SELECT t.waste_type_name\n" +
            "FROM wa_org_waste_type t\n" +
            "where t.org_id = #{orgId} )\n" +
            "and o.send_org_id in (\n" +
            "SELECT a.org_id\n" +
            "FROM wa_agreement a\n" +
            "LEFT JOIN sys_org o on a.`name` = o.real_name\n" +
            "where 1=1\n" +
            "and a.exam_state = 1\n" +
            "and o.id = #{orgId}\n" +
            "and a.expire_date > NOW())\n" +
            "and o.id = #{orderId}")
    List<Integer> getReceiveOrderWasteIdList(@Param("orderId") Integer orderId, @Param("orgId") Integer orgId);

    @Select("SELECT a.exam_state,a.org_id FROM wa_org_verify a LEFT JOIN sys_org_member b ON a.org_id = b.org_id " +
            "WHERE b.member_id = #{memberId}")
    OrgVerify isOrgVerify(Integer memberId);

    @Select("SELECT COUNT(*) FROM wa_agreement a \n" +
            "LEFT JOIN sys_org b ON a.name = b.real_name \n" +
            "LEFT JOIN sys_org_member c ON  b.id = c.org_id\n" +
            "LEFT JOIN wa_order d ON a.org_id = d.send_org_id\n" +
            "WHERE  c.member_id = #{memberId} AND unix_timestamp(a.expire_date) > unix_timestamp(NOW())\n" +
            "AND d.code = #{code}")
    Integer isAgreement(Integer memberId, String code);


    @Select("SELECT CONCAT(province,city,area,address) FROM wa_order_address WHERE order_id = #{orderId} AND type = 0")
    String getOrderStartAddress(Integer orderId);

    @Select("SELECT GROUP_CONCAT(CONCAT(name,weight * 0.01),'吨') FROM wa_order_details WHERE order_id = #{orderId}")
    String getOrderWasteInfoStr(Integer orderId);

    @Select("        SELECT GROUP_CONCAT(CONCAT( d.name,d.weight - IFNULL(x.weight,'0') * 0.01),'吨')\n" +
            "        FROM wa_order o\n" +
            "        LEFT JOIN wa_order_details d on o.id = d.order_id\n" +
            "        LEFT JOIN sys_org so ON so.id = o.send_org_id\n" +
            "        LEFT JOIN wa_org_waste ow ON ow.id = d.waste_id\n" +
            "        LEFT JOIN (\n" +
            "        SELECT d.parent_order_id,d.waste_id,d.name,sum(d.weight) as weight\n" +
            "        FROM `wa_order_details` d LEFT JOIN wa_order wo ON wo.id = d.order_id\n" +
            "        where\n" +
            "        1=1\n" +
            "        and d.parent_order_id is not null\n" +
            "        and wo.state != 10\n" +
            "        GROUP BY d.parent_order_id,d.waste_id,d.name\n" +
            "        ) x on d.order_id = x.parent_order_id and d.waste_id = x.waste_id\n" +
            "        where\n" +
            "        1=1\n" +
            "        and o.type = 0\n" +
            "        and o.receive_org_id IS null\n" +
            "        and o.state = 0\n" +
            "        and IFNULL(x.weight,'0') < d.weight\n" +
            "        and o.id = #{orderId}")
    String getRemainOrderWasteInfoStr(Integer orderId);

    @Select("SELECT code FROM wa_order WHERE id = #{orderId}")
    String getCodeById(Integer orderId);
}
